<template>
    <!-- 箱柜明细 -->
    <div class="app-container bin-partial-detail" v-if="params.table">
        <FilterForm
            :data="filterData"
            label-width="85px"
            :disabled="true"
            :btn="false"
        />

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleDialogShow"
                    >新增</el-button
                >
            </el-col>

            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    size="mini"
                    @click="handleInOpen"
                    >{{ query.status == 1 ? "撤销" : "入库" }}</el-button
                >
            </el-col>
        </el-row>

        <Table
            ref="Table"
            :api="$Api.woodPurchase.bin.detail"
            :params="params.table"
            :index="true"
            :summary="true"
            :summary-column="['pieceCount', 'inCount', 'noteCount']"
            height="70vh"
        >
            <el-table-column
                label="操作"
                align="center"
                width="120"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope"
                    ><el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click.native.stop="handleDialogShow(scope.row)"
                        v-hasPermi="['system:user:edit']"
                        >修改</el-button
                    >
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click.native.stop="handleDelete(scope.row)"
                        v-hasPermi="['system:user:remove']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>

            <el-table-column label="包号" prop="baleNum" />
            <el-table-column label="品名" prop="goodsName" />
            <el-table-column label="品牌" prop="brandName" width="160" />
            <el-table-column label="等级" prop="level" />
            <el-table-column label="仓库" prop="warehouse" width="160" />
            <el-table-column label="库位" prop="storageLocation" width="120" />
            <el-table-column label="厚度(mm)" prop="ply" />
            <el-table-column label="宽度(mm)" prop="width" />
            <el-table-column label="长度(m)" prop="length" />
            <el-table-column label="片数" prop="sliceCount" />
            <el-table-column label="件数" prop="pieceCount" />
            <el-table-column label="单包方数" prop="oneCount" />
            <el-table-column label="方数" prop="inCount" />
            <el-table-column label="单包码单" prop="oneNoteCount" />
            <el-table-column label="码单方数" prop="noteCount" />
            <el-table-column label="单价" prop="price" />
            <el-table-column label="金额" prop="money" />
            <el-table-column label="货物状态" prop="goodsStatus" />
            <el-table-column label="产地" prop="originPlace" />
            <el-table-column label="源单序号" prop="sourceOrder" />
            <el-table-column label="备注" prop="remark" width="200" />
        </Table>

        <el-dialog
            :visible.sync="dialog.show"
            :before-close="dialogClose"
            :modal-append-to-body="false"
            :close-on-click-modal="false"
            width="1000px"
        >
            <FilterForm
                ref="DialogForm"
                :data="dialog.form"
                :btn="false"
                :line="0"
                label-width="85px"
            />

            <div class="btn-group">
                <el-button type="primary" @click.native.stop="handleEdit"
                    >确认</el-button
                >
                <el-button @click.native.stop="dialogClose">关闭</el-button>
            </div>
        </el-dialog>

        <el-dialog
            title="箱柜入库"
            :visible.sync="dialog.in.show"
            :before-close="dialogClose"
            :modal-append-to-body="false"
            :close-on-click-modal="false"
            width="800px"
        >
            <FilterForm
                ref="DialogInForm"
                :data="dialog.in.form"
                :btn="false"
                :line="0"
            />

            <div class="btn-group">
                <el-button type="primary" @click.native.stop="handleIn"
                    >确认</el-button
                >
                <el-button @click.native.stop="dialogClose">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { checkNumEmpty } from "@/utils/validate";
export default {
    components: {},
    data: () => {
        return {
            params: {
                table: null,
            },
            filterData: [],
            dialog: {
                show: false,
                form: [],

                // 入库
                in: {
                    show: false,
                    form: [],
                },
            },
        };
    },
    created() {
        let { id } = this.$route.params;
        this.query = JSON.parse(sessionStorage[`binPartiaDetail_${id}`]);
        let { caseNum, ladingNum, pactNum, pactName, supplierName } =
            this.query;

        this.params.table = {
            caseNum,
            ladingNum,
            pactNum,
            page: "none",
        };

        this.filterData = [
            { label: "箱号：", name: "caseNum", value: caseNum },
            { label: "提单号：", name: "ladingNum", value: ladingNum },
            { label: " 开证合同：", name: "pactNum", value: pactNum },
            { label: " 供应商：", name: "pactNum", value: supplierName },
            { label: " 开证公司：", name: "pactNum", value: pactName },
        ];

        this.dialog.form = [
            { label: "包号：", name: "baleNum", xl: 8 },
            {
                label: "品名",
                name: "goodsName",
                type: "select",
                select: "goodsName",
                xl: 8,
                rules: [
                    {
                        required: true,
                        message: "请选择品名",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "品牌",
                name: "brandName",
                type: "select",
                select: "productName",
                xl: 8,
                rules: [
                    {
                        required: true,
                        message: "请选择品牌",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "等级",
                name: "level",
                type: "select",
                select: "levelName",
                xl: 8,
                rules: [
                    {
                        required: true,
                        message: "请选择等级",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "仓库",
                name: "warehouse",
                type: "select",
                select: "warehouse",
                xl: 8,
                rules: [
                    {
                        required: true,
                        message: "请选择仓库",
                        trigger: "blur",
                    },
                ],
            },
            { label: "库位", name: "storageLocation", xl: 8 },
            {
                label: "厚度",
                name: "ply",
                type: "number",
                xl: 8,
                onchange: this.checkOneCount,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入厚度",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "宽度",
                name: "width",
                type: "number",
                xl: 8,
                onchange: this.checkOneCount,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入宽度",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "长度",
                name: "length",
                type: "number",
                xl: 8,
                onchange: this.checkOneCount,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入长度",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "片数",
                name: "sliceCount",
                type: "number",
                xl: 8,
                onchange: this.checkOneCount,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入片数",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "件数",
                name: "pieceCount",
                type: "number",
                xl: 8,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入件数",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "单包方数",
                name: "oneCount",
                type: "number",
                value: 0,
                xl: 8,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入单包方数",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "单包码单",
                name: "oneNoteCount",
                type: "number",
                value: 0,
                xl: 8,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入单包码单",
                        trigger: "blur",
                    },
                ],
            },
            {
                label: "单价",
                name: "price",
                type: "number",
                xl: 8,
                rules: [
                    {
                        validator: checkNumEmpty,
                        message: "请输入单价",
                        trigger: "blur",
                    },
                ],
            },
        ];

        this.dialog.in.form = [
            {
                label: "入库时间",
                name: "inDate",
                span: 8,
                type: "date",
                required: true,
            },
            {
                label: "仓库",
                name: "warehouse",
                type: "select",
                select: "warehouse",
                span: 8,
                required: true,
            },
            {
                label: "费用",
                name: "money",
                span: 8,
                type: "number",
                required: true,
            },
            { label: "备注", name: "remark", span: 24, type: "textarea" },
        ];
    },
    methods: {
        handleEdit() {
            let { Table, DialogForm } = this.$refs;
            let { caseNum, ladingNum } = this.query;
            let woodPurchaseInGoods = DialogForm.get();

            DialogForm.checkForm((pass) => {
                if (pass) {
                    let api = this.$Api.woodPurchase.bin.add;
                    let postParams = {
                        caseNum,
                        ladingNum,
                        woodPurchaseInGoods: [woodPurchaseInGoods],
                    };

                    if (woodPurchaseInGoods.inId) {
                        api = this.$Api.woodPurchase.bin.update;
                        postParams = woodPurchaseInGoods;
                    }

                    api(postParams).then((res) => {
                        this.msgSuccess("操作成功");
                        this.dialogClose();
                        Table.getData();
                    });
                }
            });
        },
        handleDialogShow(row) {
            this.dialog.show = true;
            this.$nextTick(() => {
                this.$refs.DialogForm.set(JSON.parse(JSON.stringify(row)));
            });
        },
        handleDelete(row) {
            let { Table, DialogForm } = this.$refs;
            let itemId = [row.itemId].join();
            this.$confirm(
                '是否确认删除采购订单信息编号为"' + itemId + '"的数据项?',
                "警告",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            ).then(() => {
                this.$Api.woodPurchase.bin.del(itemId).then((res) => {
                    this.msgSuccess(res.msg);
                    Table.getData();
                });
            });
        },
        handleInOpen() {
            let { status } = this.query;
            if (status == 1) {
                this.handleIn();
                return false;
            }
            this.dialog.in.show = true;
        },
        handleIn() {
            let { DialogInForm, Table } = this.$refs;
            let { orderId, status } = this.query;
            let d = {};
            // 入库时做校验
            if (status != 1) {
                d = DialogInForm.get();
                if (!d.inDate) {
                    this.$message.error("请选择入库时间");
                    return false;
                }
                if (!d.warehouse) {
                    this.$message.error("请选择仓库");
                    return false;
                }
                if (!d.money) {
                    this.$message.error("请输入费用");
                    return false;
                }
            }

            let ids = [];
            Table.get().forEach((v) => {
                ids.push(v.itemId);
            });

            let postData = {
                ids,
                type: 0,
                orderId,
                repeal: status,
            };

            if (status != 1) {
                postData.inTime = d.inDate;
                postData.inMoney = d.money;
                postData.remark = d.remark;
                postData.warehouse = d.warehouse;
            }

            this.$Api.woodPurchase.bin.setState(postData).then((res) => {
                this.dialogClose();
                this.$message.success("操作成功");
                this.query.status = status == 1 ? 0 : 1;
            });
        },
        dialogClose() {
            let { DialogForm, DialogInForm } = this.$refs;
            DialogForm && DialogForm.set({});
            DialogInForm && DialogInForm.set({});
            this.dialog.show = false;
            this.dialog.in.show = false;
        },
        // 单包方数计算 ：厚度/1000*宽度/1000*长度*片数*件数
        checkOneCount(d, com) {
            let { ply, width, length, sliceCount } = d;
            if (!ply || !width || !length || !sliceCount) return false;
            let t = (ply * width * length * sliceCount) / 1000000;

            t = Number(t.toFixed(3));

            com.setValue("oneCount", t);
            !d.oneNoteCount && com.setValue("oneNoteCount", t);
        },
    },
};
</script>
<style lang="scss" scoped>
.bin-partial-detail {
    .btn-group {
        text-align: right;
    }
}
</style>